package judesart.app.authentication;

import java.util.ArrayList;
import java.util.Collection;

import judesart.app.domain.User;
import judesart.app.domain.UserRole;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

public class AuthUserDetails implements UserDetails
{
    private static final long serialVersionUID = 1L;
    private String username;
    private String password;
    private Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();

    public AuthUserDetails()
    {
        super();
    }

    public AuthUserDetails(User user)
    {
        this.username = user.getUsername();
        this.password = user.getPassword();
        for (UserRole userRole : user.getUserRoles())
        {
            this.authorities.add(new Authority(userRole.getRole().name()));
        }
    }

    public AuthUserDetails(String username, String password,
            Collection<String> authorities)
    {
        super();
        this.username = username;
        this.password = password;
        this.authorities = new ArrayList<GrantedAuthority>();
        for (String string : authorities)
        {
            this.authorities.add(new Authority(string));
        }
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities()
    {
        return authorities;
    }

    @Override
    public String getPassword()
    {
        return password;
    }

    @Override
    public String getUsername()
    {
        return username;
    }

    @Override
    public boolean isAccountNonExpired()
    {
        return true;
    }

    @Override
    public boolean isAccountNonLocked()
    {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired()
    {
        return true;
    }

    @Override
    public boolean isEnabled()
    {
        return true;
    }

}
